package com.h3bpm.kumhosunny.sample.controller.utils;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
/**
 * @author : wjq
 * @Date: 2019/9/23 18:37
 * @Description:
 */
@Component
public class NemberUtil {
    /**
     *
     * 功能描述:  key 为 当日生成的数值  value为项目单编码
     *
     * @param:
     * @return:
     * @auther: Administrator
     * @date: 2019/9/27 11:20
     */
    public final static Map<String ,Integer> MAP_SAMPLE = new HashMap();
/**
 *
 * 功能描述: // 把生成的数值放入
 *
 * @param:
 * @return:
 * @auther: Administrator
 * @date: 2019/9/27 11:20
 */

    public static void putMap(String key, Integer value) {
        if (key == null) {
            return;
        }
        MAP_SAMPLE.put(key, value);
    }
/**
 *
 * 功能描述: //取出
 *
 * @param:
 * @return:
 * @auther: Administrator
 * @date: 2019/9/27 11:21
 */

    public static Integer getMap(String key) {

        if (key.isEmpty()) {
            return null;
        }
        //通过entrySet()方法把map中的每个键值对变成对应成Set集合中的一个对象
        Set set = MAP_SAMPLE.entrySet();
        Iterator<Map.Entry<String,Integer >> iterator = set.iterator();
        ArrayList<Integer> arrayList = new ArrayList();
        while (iterator.hasNext()) {
            //Map.Entry是一种类型，指向map中的一个键值对组成的对象
            Map.Entry<String,Integer> entry = iterator.next();
            if (entry.getKey().equals(key)) {
                arrayList.add(entry.getValue());
            }
        }
        if (arrayList.isEmpty()) {
            return 0;
        }
        Integer max = Collections.max(arrayList);
        return max;
    }

    public static void clearMap() {
        MAP_SAMPLE.clear();
    }
/**
 *
 * 功能描述: //数值转化4位str
 *
 * @param:
 * @return:
 * @auther: Administrator
 * @date: 2019/9/27 11:21
 */
    public static String nemberStr4(Integer num) {
        if (num == null) {
            String str = String.format("%04d", 0);
            return str;
        }
        String str = String.format("%04d", num);
        return str;
    }
    /**
     *
     * 功能描述: //数值转化4位str
     *
     * @param:
     * @return:
     * @auther: Administrator
     * @date: 2019/9/27 11:21
     */
    public static String nemberStr2(Integer num) {
        if (num == null) {
            String str = String.format("%02d", 0);
            return str;
        }
        String str = String.format("%02d", num);
        return str;
    }
    /**
     *
     * 功能描述: //拼接需求的项目单号
     *
     * @param:
     * @return:
     * @auther: Administrator
     * @date: 2019/9/27 11:21
     */
    public static String itemNember(String itemCode,String nemberStr) {
        if (itemCode == null) {
            return null;
        }
        String time = DateUtil.getCurrDateTime();
        return itemCode+DateUtil.getTimeFromyyMm(time)+nemberStr;
    }

}
